﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace FunCam
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window, INotifyPropertyChanged
    {
        #region FileFormats (INotifyPropertyChanged Property)
        private ObservableCollection<string> _fileFormats;

        public ObservableCollection<string> FileFormats
        {
            get { return _fileFormats; }
            set
            {
                _fileFormats = value;
                RaisePropertyChanged("FileFormats");
            }
        }
        #endregion
        
        public SettingsWindow()
        {
            InitializeComponent();
            this.DataContext = this;

            FileFormats = new ObservableCollection<string>();
            FileFormats.Add("png");
            FileFormats.Add("jpg");
            FileFormats.Add("gif");
            FileFormats.Add("bmp");
            FileFormats.Add("tif");

            var s = Properties.Settings.Default;
            tbAutoSaveInterval.Text = s.AutoSaveInterval.ToString();
            tbFilenameBase.Text = s.SaveBasename;
            tbSaveLocation.Text = s.SaveLocation;
            cmbSaveFormat.SelectedItem = s.SaveFormat;
            cbPlayShutterSound.IsChecked = s.PlayShutterSound;
        }

        private void OkBtn_Click(object sender, RoutedEventArgs e)
        {
            var s = Properties.Settings.Default;
            s.AutoSaveInterval = int.Parse(tbAutoSaveInterval.Text);
            s.SaveBasename = tbFilenameBase.Text;
            s.SaveLocation = tbSaveLocation.Text;
            s.SaveFormat = cmbSaveFormat.SelectedValue.ToString();
            s.PlayShutterSound = cbPlayShutterSound.IsChecked.Value;
            s.Save();

            this.Close();
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #region INotifyPropertyChanged values

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            using (var dlg = new CommonOpenFileDialog())
            {
                dlg.InitialDirectory = Properties.Settings.Default.SaveLocation;
                dlg.EnsurePathExists = true;
                dlg.IsFolderPicker = true;
                if( dlg.ShowDialog() == CommonFileDialogResult.Ok )
                    tbSaveLocation.Text = dlg.FileName;
            }
        }
    }
}
